(function(win,$){

	var userId= getQueryParamValue("userId");
	$(document).ready(function(){
		//判断是否为车主
		$.ajax({
			headers: {'Cookie' : document.cookie },
			method : 'get',
			url : rootPath+'/customer/api/v1/queryUserOwner',
			error:function(XMLHttpRequest, ajaxOptions, thrownError){
				//alert("数据刷新失败");
			},
			success : function(resultData){
				if(resultData.length==0){
					//不是会员
					alert("请先注册成会员");
					//window.location = rootPath + "/views/homepage/signUp.html";
					window.location = rootPath + "/views/modules/register.html";
				}else{
					//是会员
				}
			}
		});


		/*去掉iphone手机滑动默认行为*/
//		$('body').on('touchmove', function (event) {
//		    event.preventDefault();
//		});
		var rootPath = getRootPath();
		var dealerCode= getQueryParamValue("dealerCode");

//		$("#nowDealerDiv").hide();
//		$("#maintainDealerDiv").hide();
//		$("#level2").hide();
//		$("#level3").hide();
		//监听城市选择页面搜索输入框
		$("#cha").keyup(function(event){
	        if (event.keyCode === 13){
	       	 searchCity();
	        }
	      });

		//监听经销商选择页面搜索输入框
		$("#sou").keyup(function(event){
	        if (event.keyCode === 13){
	       	 searchDealer();
	        }
	      });

		$("#imgShow").hide();
		$("#inputShow").hide();
	})

	function addCarInfo(){
		$("#level1").hide();
		$("#level2").show();
	}

	// 随机数
	  function randomNum(min,max){
		    return Math.floor( Math.random()*(max-min)+min);
		  }
	  // 随机验证码字体颜色
	  function randomColor(min,max){
		    var r = randomNum(min,max);
		    var g = randomNum(min,max);
		    var b = randomNum(min,max);
		    return "rgb("+r+","+g+","+b+")";
		  }
	  // 生成验证码图片
	  function drawPic(string){
		    var canvas=document.getElementById("canvas");
		    var width=canvas.width;
		    var height=canvas.height;
		    var ctx = canvas.getContext('2d');
		    ctx.textBaseline = 'bottom';

		    /**绘制背景色**/
		    ctx.fillStyle = randomColor(180,240); //颜色若太深可能导致看不清
		    ctx.fillRect(0,0,width,height);

		    /**绘制文字**/
		    var str = string || 'ABCF';
		    for(var i=0; i<4; i++){
		      //var txt = str[randomNum(0,str.length)];
		      var txt = str[i];
		      ctx.fillStyle = randomColor(50,160);  //随机生成字体颜色
		      ctx.font = randomNum(15,40)+'px SimHei'; //随机生成字体大小
		      var x = 10+i*25;
		      var y = randomNum(25,45);
		      var deg = randomNum(-45, 45);
		      //修改坐标原点和旋转角度
		      ctx.translate(x,y);
		      ctx.rotate(deg*Math.PI/180);
		      ctx.fillText(txt, 0,0);
		      //恢复坐标原点和旋转角度
		      ctx.rotate(-deg*Math.PI/180);
		      ctx.translate(-x,-y);
		    }
		    /**绘制干扰线**/
/*		    for(var i=0; i<8; i++){
		      ctx.strokeStyle = randomColor(40,180);
		      ctx.beginPath();
		      ctx.moveTo( randomNum(0,width), randomNum(0,height) );
		      ctx.lineTo( randomNum(0,width), randomNum(0,height) );
		      ctx.stroke();
		    }*/
		    /**绘制干扰点**/
		    for(var i=0; i<100; i++){
		      ctx.fillStyle = randomColor(0,255);
		      ctx.beginPath();
		      ctx.arc(randomNum(0,width),randomNum(0,height), 1, 0, 2*Math.PI);
		      ctx.fill();
		    }
		  }
	function next(){
		var isImgvalidate = false
		var val = $("#imgValidate").val().trim()
		var vin = $("#vin").val().trim()
		var engineNo = $("#engineNo").val().trim()
	
		if(vin.length < 6){
			alert("请输入车架号后六位！")
			return !1
		}else if(/[^0-9a-zA-Z]/g.test(vin)){
			alert('车架号格式不正确！')
			return !1
		}else{}
		
		
		if(engineNo.length < 4){
			alert("请输入发动机号后四位！")
			return !1
		}else if(/[^0-9a-zA-Z]/g.test(engineNo)){
			alert('发动机号格式不正确！')
			return !1
		}else{} 
		
		
		if(isImgvalidate && val.length < 4){
			alert('请输入四位图片验证码！')
			return !1
		}else if(/[^0-9a-zA-Z]/g.test(val)){
			alert('验证码格式不正确！')
			return !1
		}else{} 
		
		//2017-04-07   liyh1  添加加载层 并  灰色按钮
		layer.load(1,{shade:false});
		$("#next").attr("disabled","disabled");
		$("#next").attr("class","btnbtn1");
		$("#next").removeAttr("style");
		var carInfoDto = {
				"vin": $("#vin").val().trim(),
				"engine": $("#engineNo").val().trim(),
				"userId": userId,
				"randomcode":$("#imgValidate").val().trim()

		}
		//alert(carInfoDto.vin+','+carInfoDto.engineNo);
		$.ajax({
			headers: {'Cookie' : document.cookie },
			type : "POST",
			url : rootPath + "/customer/api/v1/carInfo",
			dataType : "json",
			contentType : "application/json",
			data : JSON.stringify(carInfoDto),
			success : function(resultDate) {

	        	var datas=eval(resultDate);
	        	if(datas.resultCode==0){
	        		$("#next").removeAttr("disabled");
	        		$("#next").attr("class","btnbtn");
	        		layer.closeAll('loading');
	        		return alert(datas.errMsg||"数据库异常,请重新操作！")
	        	}

	        	if(datas.data.flag=='1'){  // 绑定成功
		        	alert(datas.data.resultMsg);
	        		//获取默认经销商信息
	        		$.ajax({
	        			headers: {'Cookie' : document.cookie },
	        			method : 'get',
	        			url : rootPath+'/customer/api/v1/dealerInfo?regionName='+ $("#regionName").val()+'&latitude='+$("#latitude").val()+'&longitude='+$("#longitude").val(),
	        	        dataType:"json",
	        	        async: false,
	        	        contentType:"application/json",
	        	        data:null,
	        			error:function(XMLHttpRequest, ajaxOptions, thrownError){
	        				alert("获取默认销售店信息失败!");
	        			},
	        			success : function(resultData){
	        				$.each(resultData,function(maintainSelIndex,maintainSelObj){
	        					$("#dealerCode").val(maintainSelObj.dealerCode);
	        					//var dealerName = maintainSelObj.dealerShortName;
	        					//var detailAddress = maintainSelObj.detailAddress;
	        					$("#dealerName").html(maintainSelObj.dealerShortName);
	        					$("#detailAddress").html(maintainSelObj.detailAddress);

	        				});
	        			}
	        		});
					//window.location = rootPath + "/views/hpp.html";
	    			$("#level2").hide();
	    			$("#level3").show();
	    			layer.closeAll('loading');
	        	}else if(datas.data.flag=='0'){
	        		alert(datas.data.resultMsg);
	        		$("#next").removeAttr("disabled");
	        		$("#next").attr("class","btnbtn");
	        		layer.closeAll('loading');
	        	}else if(datas.data.flag=='2'){
	        		alert(datas.data.resultMsg);
	        		$("#next").removeAttr("disabled");
	        		$("#next").attr("class","btnbtn");
	        		layer.closeAll('loading');
	        		$("#imgShow").show();
	        		$("#inputShow").show();
	        		drawPic(datas.data.random);
	        		if(!isImgvalidate){
		        		  document.getElementById("changeImg").onclick = function(e){
		        			    e.preventDefault();
			        			$.ajax({
				        			headers: {'Cookie' : document.cookie },
				        			method : 'get',
				        			url : rootPath+'/customer/api/v1/randomCode',
				        	        dataType:"json",
				        	        async: false,
				        	        contentType:"application/json",
				        	        data:null,
				        			error:function(XMLHttpRequest, ajaxOptions, thrownError){
				        				alert("获取默认销售店信息失败!");
				        			},
				        			success : function(resultData){
				        				drawPic(resultData.data);
				        			}
			        			})

		        		   }
	        		}
	        		isImgvalidate = true
	        	}else{
	        		//location.reload();
	        		$("#next").removeAttr("disabled");
	        		$("#next").attr("class","btnbtn");
	        		layer.closeAll('loading');
	        	}
			},
			error : function(data) {
				//alert("认证失败！");
			}
		});
		layer.closeAll('loading');
	}
	//定位所在地经销商,点击
	function selectNowDealer(obj){
		 $("#headImg").hide();
		 $("#sou").val("");
		 $("#firstImg").hide();
		 $("#maintainDealerDiv").hide();
		 $("#maintainDealerDiv").removeClass("active in");
		 $("#level3").hide();
		 $("#nowDealerDiv").show();
		 $("#dealerDiv").html("");
		 //初始化界面
		 $("#nowDealerDiv").addClass("active in");
			//根据编号获取所在地经销商列表
				$.ajax({
					headers: {'Cookie' : document.cookie },
					method : 'get',
					url : rootPath+'/maintain/api/v1/maintainDealer?regionName='+ $("#regionName").val()+'&dealerCode='+dealerCode,
					error:function(XMLHttpRequest, ajaxOptions, thrownError){
						//alert("未连接服务器!");
					},
					success : function(resultData){
						var wholeHtml = '';
						var  Km = '';
						$.each(resultData,function(maintainDealerIndex,maintainDealerObj){

							var findDealerHtml = '';
							var lat1 = maintainDealerObj.latitude;
							var lng1 = maintainDealerObj.longitude;
							var lat2 = $("#latitude").val();
							var lng2 = $("#longitude").val();
							Km = GetDistance(lat1,lng1,lat2,lng2).toFixed(2);
							if(lat1==null||lat1=="0"||lng1==null||lng1=="0"){var distance = "暂无";}
							else{var distance = Km + '千米';}

								if(maintainDealerObj.remark =='首选店'){
									$("#nameFirst").html(maintainDealerObj.dealerName);
									$("#distanceFirst").html(distance);
									$("#addrFirst").html(maintainDealerObj.detailAddress);
									$("#codeFirst").val(maintainDealerObj.dealerCode);
									$('#Tel').attr('href',maintainDealerObj.serviceHotline);
									$("#remarkFirst").html(maintainDealerObj.remark);
								}else{
									$("#KmFirst").hide();
							findDealerHtml = findDealerHtml + '<li data-id="'+ Km +'">'
															+ '<div class="panel panel-default">'
															+ '<table style="width:100%" class="table-condensed"><tbody>'
															+ '<tr>'
															+ '<td style="width:160px;text-align:left;"><a onclick="gmcc.sureDealer('+maintainDealerIndex+')">'
															+ '<p id="name'+ maintainDealerIndex +'" style="font-family:黑体;font-size:16px;color:#000000">' + maintainDealerObj.dealerName + '</p>'
															+ '<p class="hidden" id="shortName'+maintainDealerIndex+'">'+maintainDealerObj.dealerShortName+'</p>'
															+ '</a></td>'
															+ '<td style="width:100px;text-align:center;font-size:14px;color:#7a7a7a">'
															+ '<span>距离：</span><span>'+ distance + '</span>'
															+ '</td>'
															+ '</tr><tr>'
															+ '<td style="width:160px;text-align:left;">'
															+ '<span id="addr'+ maintainDealerIndex +'" style="font-faminly:宋体;font-size:15px;color:#615f5f">' + maintainDealerObj.detailAddress + '</span>'
															+ '</td>'
															+ '<td style="width:100px;text-align:center;">'
															+ '<a href="tel:'+ maintainDealerObj.serviceHotline +'" id="Tel">'
															+ '<span class="glyphicon glyphicon-earphone" style="color: rgb(255, 0, 23); font-size: 12px;"></span>'
															+ '<span style="font-family:宋体;font-size:14px;color:#7a7a7a"> 电话</span>'
															+ '</a></td>'
															+ '</tr>'
															+ '</tbody></table>'
															+ '<input id="code'+ maintainDealerIndex +'" class="hidden" value="' + maintainDealerObj.dealerCode + '">'
															+ '</div></li>'

							wholeHtml += findDealerHtml;
							}
						});
						$("#dealerDiv").append(wholeHtml);
					}
				});
	}
	function sureDealer(obj){
		if(obj=='-1'){
			  var code = $("#codeFirst").val();
		 	  var name = $("#shortNameFirst").html();
		   	  var addr = $("#addrFirst").html();
			}else{
			  var code = $("#code"+obj).val();
			  var name = $("#shortName"+obj).html();
			  var addr = $("#addr"+obj).html();
			}
		//alert(code+','+name+','+addr);
		$("#dealerCode").val(code);
		$("#dealerName").html(name);
		$("#detailAddress").html(addr);
		$("#nowDealerDiv").hide();
		$("#nowDealerDiv").removeClass("active in");
		 $("#level3").show();
		 $("#headImg").show();
		 $("#list2").show();
		 $("#searchDealer").html("");
		//选择销售店后，清楚之前选择的顾问
			$("#salesConsultantPhone").val("");
			$("#salesConsultantId").val("");
			$("#counselorName").html("");
	}
	//排序
	function Sort(bo)
	{
	    var arr = document.getElementById("list2").innerHTML.match(/<li>[\s\S]+?<\/li>/g);
	    arr.sort(function(a,b){
	        a = parseInt(/\d+/.exec(a)[0], 10);
	        b = parseInt(/\d+/.exec(b)[0], 10);
	        return bo?b-a:a-b;
	    });
	    document.getElementById("ol1").innerHTML = arr.join("");
	}
	//进行经纬度转换为距离的计算

	function Rad(d){
	  return d * Math.PI / 180.0;//经纬度转换成三角函数中度分表形式。
	}
	//计算距离，参数分别为第一点的纬度，经度；第二点的纬度，经度
	function GetDistance(lat1,lng1,lat2,lng2){

	   var radLat1 = Rad(lat1);
	   var radLat2 = Rad(lat2);
	   var a = radLat1 - radLat2;
	   var  b = Rad(lng1) - Rad(lng2);
	   var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
	   Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
	   s = s *6378.137 ;// EARTH_RADIUS;
	   s = Math.round(s * 10000) / 10000; //输出为公里
	   //s=s.toFixed(4);
	   return s;
	}
	function selectDealer(obj){
		$("#sou").val("");
		$("#headImg").hide();
		var code = $("#"+obj).val();
		$("#regionName").val(code);
		$("#maintainDealerDiv").hide();
		$("#maintainDealerDiv").removeClass("active in");
		$("#dealerDiv").html("");
		 $("#nowDealerDiv").show();
		 $("#searchCity").html("");
		 $("#searchDealer").html("");
		 $("#list2").show();
		 //初始化界面
		 $("#nowDealerDiv").addClass("active in");
		 for(var i=0;i<26;i++){
				var en = String.fromCharCode((65+i));
				$("#"+en).html("");
		 }
		//根据编号获取所在地经销商列表
		$.ajax({
			headers: {'Cookie' : document.cookie },
			method : 'get',
			url : rootPath+'/maintain/api/v1/maintainDealer?regionName='+code+'&dealerCode='+dealerCode,
			error:function(XMLHttpRequest, ajaxOptions, thrownError){
				//alert("未连接服务器!");
			},
			success : function(resultData){

				var wholeHtml = '';
				var  Km = '';
				$.each(resultData,function(maintainDealerIndex,maintainDealerObj){

					var findDealerHtml = '';
					var lat1 = maintainDealerObj.latitude;
					var lng1 = maintainDealerObj.longitude;
					var lat2 = $("#latitude").val();
					var lng2 = $("#longitude").val();
					Km = GetDistance(lat1,lng1,lat2,lng2).toFixed(2);
					if(lat1==null||lat1=="0"||lng1==null||lng1=="0"){var distance = "暂无";}
					else{var distance = Km + '千米';}

						if(maintainDealerObj.remark =='首选店'){
							$("#nameFirst").html(maintainDealerObj.dealerName);
							$("#distanceFirst").html(distance);
							$("#addrFirst").html(maintainDealerObj.detailAddress);
							$("#codeFirst").val(maintainDealerObj.dealerCode);
							$('#Tel').attr('href',maintainDealerObj.serviceHotline);
							$("#remarkFirst").html(maintainDealerObj.remark);
						}else{
							$("#KmFirst").hide();
					findDealerHtml = findDealerHtml + '<li data-id="'+ Km +'">'
													+ '<div class="panel panel-default">'
													+ '<table style="width:100%" class="table-condensed"><tbody>'
													+ '<tr>'
													+ '<td style="width:160px;text-align:left;"><a onclick="gmcc.sureDealer('+maintainDealerIndex+')">'
													+ '<p id="name'+ maintainDealerIndex +'" style="font-family:黑体;font-size:16px;color:#000000">' + maintainDealerObj.dealerName + '</p>'
													+ '<p class="hidden" id="shortName'+maintainDealerIndex+'">'+maintainDealerObj.dealerShortName+'</p>'
													+ '</a></td>'
													+ '<td style="width:100px;text-align:center;font-size:14px;color:#7a7a7a">'
													+ '<span>距离：</span><span>'+ distance + '</span>'
													+ '</td>'
													+ '</tr><tr>'
													+ '<td style="width:160px;text-align:left;">'
													+ '<span id="addr'+ maintainDealerIndex +'" style="font-faminly:宋体;font-size:15px;color:#615f5f">' + maintainDealerObj.detailAddress + '</span>'
													+ '</td>'
													+ '<td style="width:100px;text-align:center;">'
													+ '<a href="tel:'+ maintainDealerObj.serviceHotline +'" id="Tel">'
													+ '<span class="glyphicon glyphicon-earphone" style="color: rgb(255, 0, 23); font-size: 12px;"></span>'
													+ '<span style="font-family:宋体;font-size:14px;color:#7a7a7a"> 电话</span>'
													+ '</a></td>'
													+ '</tr>'
													+ '</tbody></table>'
													+ '<input id="code'+ maintainDealerIndex +'" class="hidden" value="' + maintainDealerObj.dealerCode + '">'
													+ '</div></li>'

					wholeHtml += findDealerHtml;
					}
				});
				$("#dealerDiv").append(wholeHtml);
			}
		});
	}
	//选择经销商，点击
	function selectMaintainDealer(obj){
		$("#headImg").hide();
		$("#cha").val("");
		$("#dealerMap").show();
		$("#KmFirst").hide();
		$("#nowDealerDiv").hide();
		$("#nowDealerDiv").removeClass("active in");
		 $("#maintainDealerDiv").show();
		 //初始化界面
		 $("#maintainDealerDiv").addClass("active in");

			// 选择经销商城市信息取得。
			$.ajax({
				headers: {'Cookie' : document.cookie },
				method : 'get',
				url : rootPath+'/maintain/api/v1/maintainDealerMap?regionType=90011002',
				error:function(XMLHttpRequest, ajaxOptions, thrownError){
					//alert("获取失败!");
				},
				success : function(resultData){

					var wholeHtml = '';

					var city = '';
					$.each(resultData,function(maintainDealerMapIndex,maintainDealerMapObj){
						var DealerMapHtml = '';
						var str = maintainDealerMapObj.regionName;
						if(str == "") return;
						var arrRslt = makePy(str);
						var arr = arrRslt[0].substring(0,1);
						for(var i=0;i<26;i++){
							var en = String.fromCharCode((65+i));
							if(arr== en){
								city = maintainDealerMapObj.regionName;
								DealerMapHtml = DealerMapHtml + '<li>'
															  + '<a onclick="gmcc.selectDealer('+ maintainDealerMapIndex +')">'
															  + '<span style="font-size:18px;color:#7a7a7a">' + city + '</span>'
															  + '<input type="hidden" id="'+maintainDealerMapIndex + '" value="'+ maintainDealerMapObj.regionName +'">'
															  + '</a>'
															  + '</li>'
															  + '<div style="height:1px;margin:0px auto;padding:0px;background-color:#F4F4F4;overflow:hidden;"></div>'

								$("#"+en).append(DealerMapHtml);
							}
						}

					});

				}
			});
	}
	//搜索城市
	function searchCity(){
		$("#dealerMap").hide();
		$("#searchCity").html("");
		var regionName = $("#cha").val();
		$.ajax({
			headers: {'Cookie' : document.cookie },
		    type:"GET",
		    url:rootPath+"/maintain/api/v1/maintainSelectCity?regionName="+regionName,
		    dataType:"json",
		    async: false,
		    contentType:"application/json",
		    data:null,
		    success:function(data){
				var wholeHtml = '<ul class="list-group">';

				$.each(data,function(maintainSearchCityIndex,maintainSearchCityObj){
					var DealerMapHtml = '';

						DealerMapHtml = DealerMapHtml   + '<li class="list-group-item">'
														+ '<a onclick="gmcc.selectDealer('+ maintainSearchCityIndex +')">'
														+ '<span style="font-size:18px;color:#7a7a7a">' + maintainSearchCityObj.regionName + '</span>'
														+ '<input type="hidden" id="'+maintainSearchCityIndex + '" value="'+ maintainSearchCityObj.regionName +'">'
														+ '</a>'
														+ '</li>'
					       wholeHtml += DealerMapHtml;
				});
				wholeHtml = wholeHtml + '</ul>'
				$("#searchCity").append(wholeHtml);
		    },
		    error:function(data){
		    	//alert("数据刷新失败，请联系管理员！");
		    }
		 });
	}
	//搜索经销商
	function searchDealer(){
		$("#list2").hide();
		$("#searchDealer").html("");
		var dealerName = $("#sou").val();
		var regionName = $("#regionName").val();
		$.ajax({
			headers: {'Cookie' : document.cookie },
		    type:"GET",
		    url:rootPath+"/maintain/api/v1/searchDealer?dealerName="+dealerName,
		    dataType:"json",
		    async: false,
		    contentType:"application/json",
		    data:null,
		    success:function(data){
				var wholeHtml = '<ul class="list-group">';
				var  Km = '';
				$.each(data,function(SearchDealerIndex,SearchDealerObj){
					var DealerMapHtml = '';
					var findDealerHtml = '';
					var lat1 = SearchDealerObj.latitude;
					var lng1 = SearchDealerObj.longitude;
					var lat2 = $("#latitude").val();
					var lng2 = $("#longitude").val();
					Km = GetDistance(lat1,lng1,lat2,lng2).toFixed(2);
					if(lat1==null||lat1=="0"||lng1==null||lng1=="0"){var distance = "暂无";}
					else{var distance = Km + '千米';}

						DealerMapHtml = DealerMapHtml   + '<li class="list-group-item">'
														+ '<table style="width:100%" class="table-condensed"><tbody>'
														+ '<tr>'
														+ '<td style="width:160px;text-align:left;"><a onclick="gmcc.sureDealer('+SearchDealerIndex+')">'
														+ '<p id="name'+ SearchDealerIndex +'" style="font-family:黑体;font-size:16px;color:#000000">' + SearchDealerObj.dealerName + '</p>'
														+ '<p class="hidden" id="shortName'+SearchDealerIndex+'">'+SearchDealerObj.dealerShortName+'</p>'
														+ '</a></td>'
														+ '<td style="width:100px;text-align:center;font-size:14px;color:#7a7a7a">'
														+ '<span>距离：</span><span>'+ distance + '</span>'
														+ '</td>'
														+ '</tr><tr>'
														+ '<td style="width:160px;text-align:left;">'
														+ '<span id="addr'+ SearchDealerIndex +'" style="font-faminly:宋体;font-size:15px;color:#615f5f">' + SearchDealerObj.detailAddress + '</span>'
														+ '</td>'
														+ '<td style="width:100px;text-align:center;">'
														+ '<a href="tel:'+ SearchDealerObj.serviceHotline +'" id="Tel">'
														+ '<span class="glyphicon glyphicon-earphone" style="color: rgb(255, 0, 23); font-size: 12px;"></span>'
														+ '<span style="font-family:宋体;font-size:14px;color:#7a7a7a"> 电话</span>'
														+ '</a></td>'
														+ '</tr>'
														+ '</tbody></table>'
														+ '<input id="code'+ SearchDealerIndex +'" class="hidden" value="' + SearchDealerObj.dealerCode + '">'
														+ '</li>'

						wholeHtml += DealerMapHtml;
				});
				wholeHtml = wholeHtml + '</ul>'
				$("#searchDealer").append(wholeHtml);
		    },
		    error:function(data){
		    	//alert("数据刷新失败，请联系管理员！");
		    }
		 });
	}
	//服务顾问选择
	function selectCounselor(){
		$("#seviceUser").html("");
		//服务顾问信息取得
		$.ajax({
			headers: {'Cookie' : document.cookie },
		    type:"GET",
		    url:rootPath+"/customer/api/v1/counselors?positionId=602&dealerCode="+$("#dealerCode").val(),
		    dataType:"json",
		    async: false,
		    contentType:"application/json",
		    data:null,
		    success:function(data){
				var wholeHtml = '';
				$.each(data,function(carModelIndex,advisersObj){
					var counselorsHtml = '';

					counselorsHtml = counselorsHtml + '<div class="radio" onclick="gmcc.addCounseor('+carModelIndex+')">'
													+ '<label>'
													+ '<input name="SelRadio" type="radio" id="coun'+carModelIndex+'" value="'+advisersObj.userName+'">'+advisersObj.userName
													+ '<input class="hidden" type="text" id="salePhone'+carModelIndex+'" value="'+advisersObj.phone+'">'
													+ '<input class="hidden" type="text" id="saleUserId'+carModelIndex+'" value="'+advisersObj.userId+'">'
													+ '</label>'
													+ '</div>'

					wholeHtml += counselorsHtml;
				});
				$("#seviceUser").append(wholeHtml);
		    },
		    error:function(data){
		    	//alert("数据刷新失败，请联系管理员！");
		    }
		 });

		$('#counselorModal').modal({
		});
	};
	function addCounseor(obj){
		//alert(obj);
		$("#salesConsultantPhone").val($("#salePhone"+obj).val());
		$("#salesConsultantId").val($("#saleUserId"+obj).val());
		//$("#Counselor").html($("#seviceUser").find("option:selected").attr("userName"));
		$("#counselorName").html($("#coun"+obj).val());
		$("#conselorBtn").click();
	};

	function sure(){
		$("#sure").attr("disabled","disabled");
		$("#sure").attr("class","btnbtn1");
		$("#sure").removeAttr("style");

		 var dealerDto = {"dealerCode":$("#dealerCode").val()}
		 $.ajax({
				headers: {'Cookie' : document.cookie },
				type : "POST",
				url : rootPath + "/customer/api/v1/dealerCode",
				dataType : "json",
				contentType : "application/json",
				data : JSON.stringify(dealerDto),
				success : function(resultDate) {
				},
				error : function(data) {
				}
			});
		 var CounselorDto ={"tmUsersId":$("#salesConsultantId").val()}
	    	$.ajax({
	    		headers: {'Cookie' : document.cookie },
			    type:"POST",
			    url:rootPath+"/customer/api/v1/updateCounselorInfo",
			    dataType:"json",
			    contentType:"application/json",
			    data:JSON.stringify(CounselorDto),
			    success:function(data){
					//alert("保存成功");
					//window.location = rootPath + "/views/hpp.html";
			    },
			    error:function(data){
			    	//alert("保存失败！");
			    }
			  });
		window.location = "http://carowner.yonyouauto.com/wx/pub/personal?"+document.cookie;
	}





	win.gmcc = {
			"addCarInfo":addCarInfo,
			"next":next,
			"selectNowDealer":selectNowDealer,
			"sureDealer":sureDealer,
			"Sort":Sort,
			"Rad":Rad,
			"GetDistance":GetDistance,
			"selectDealer":selectDealer,
			"selectMaintainDealer":selectMaintainDealer,
			"searchCity":searchCity,
			"searchDealer":searchDealer,
			"sure":sure,
			"selectCounselor":selectCounselor,
			"addCounseor":addCounseor,
			"randomNum":randomNum,
			"randomColor":randomColor,
			"drawPic":drawPic
	}

})(window,jQuery)
